//
//  GAKKINetworkLogger.m
//  GANGAIKit
//
//  Created by fh on 2019/11/15.
//  Copyright © 2019 cashchina. All rights reserved.
//

#import "GAKKINetworkLogger.h"
#import "GAKKIConfig.h"
#import <YYModel/YYModel.h>

@implementation GAKKINetworkLogger

- (void)easyLogDebugInfoWithUrl:(NSString *)url originRequestParams:(NSDictionary *)originRequestParams actualRequestParams:(NSDictionary *)actualRequestParams responseObject:(id)responseObject error:(NSError *)error {
    if (self.shouldPrintNetLog) {
        if (self.easyPrintNetLog) {
            DBLog(@"网络请求的API: %@\n网络请求未处理的初始参数:\n%@\n网络请求的最终参数:\n%@\n网络请求响应数据:\n%@\n网络请求响应错误信息:\n%@\n",url,[originRequestParams yy_modelDescription],actualRequestParams,[responseObject yy_modelDescription],error);
        }
    }
}

/// 打印请求信息
/// @param request 请求
/// @param apiName 请求链接
/// @param originRequestParams 源参数
/// @param actualRequestParams 实际参数
- (void)logDebugInfoWithRequest:(NSURLRequest *)request apiName:(NSString *)apiName originRequestParams:(NSDictionary *)originRequestParams actualRequestParams:(NSDictionary *)actualRequestParams {
    if (self.shouldPrintNetLog) {
        NSMutableString *logString = nil;
        logString = [NSMutableString stringWithString:@"\n\n********************************************************\nRequest Start\n********************************************************\n\n"];
        
        [logString appendFormat:@"API Name:\t\t%@\n", apiName];
        [logString appendFormat:@"Method:\t\t\t%@\n", request.HTTPMethod];
        [self mutableString:logString appendURLRequest:request originRequestParams:originRequestParams actualRequestParams:actualRequestParams];
        [logString appendFormat:@"\n\n********************************************************\nRequest End\n********************************************************\n\n\n\n"];
        DBLog(@"%@", logString);
    }
}

/// 打印响应信息
/// @param response 网络响应
/// @param responseObject 响应数据对象
/// @param responseString 响应数据对象字符串
/// @param request 请求
/// @param originRequestParams 源参数
/// @param actualRequestParams 实际参数
/// @param error 错误信息
- (void)logDebugInfoWithResponse:(NSHTTPURLResponse *)response responseObject:(id)responseObject responseString:(NSString *)responseString request:(NSURLRequest *)request originRequestParams:(NSDictionary *)originRequestParams actualRequestParams:(NSDictionary *)actualRequestParams error:(NSError *)error {
    if (self.shouldPrintNetLog) {
        NSMutableString *logString = nil;
        BOOL isSuccess = error ? NO : YES;
        
        logString = [NSMutableString stringWithString:@"\n\n=========================================\nAPI Response\n=========================================\n\n"];
        
        [logString appendFormat:@"Status:\t%ld\t(%@)\n\n", (long)response.statusCode, [NSHTTPURLResponse localizedStringForStatusCode:response.statusCode]];
        [logString appendFormat:@"Content:\n\t%@\n\n", responseString];
        [logString appendFormat:@"Request URL:\n\t%@\n\n", request.URL];
        [logString appendFormat:@"Request Data:\n\t%@\n\n",originRequestParams];
        if ([responseObject isKindOfClass:[NSData class]]) {
            [logString appendFormat:@"Raw Response String:\n\t%@\n\n", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]];
        } else {
            [logString appendFormat:@"Raw Response String:\n\t%@\n\n", responseObject];
        }
        [logString appendFormat:@"Raw Response Header:\n\t%@\n\n", response.allHeaderFields];
        if (isSuccess == NO) {
            [logString appendFormat:@"Error Domain:\t\t\t\t\t\t\t%@\n", error.domain];
            [logString appendFormat:@"Error Domain Code:\t\t\t\t\t\t%ld\n", (long)error.code];
            [logString appendFormat:@"Error Localized Description:\t\t\t%@\n", error.localizedDescription];
            [logString appendFormat:@"Error Localized Failure Reason:\t\t\t%@\n", error.localizedFailureReason];
            [logString appendFormat:@"Error Localized Recovery Suggestion:\t%@\n\n", error.localizedRecoverySuggestion];
        }
        
        [logString appendString:@"\n---------------  Related Request Content  --------------\n"];
        
        [self mutableString:logString appendURLRequest:request originRequestParams:originRequestParams actualRequestParams:actualRequestParams];
        
        [logString appendFormat:@"\n\n=========================================\nResponse End\n=========================================\n\n"];
        DBLog(@"%@", logString);
    }
}

- (void)mutableString:(NSMutableString *)mutableString appendURLRequest:(NSURLRequest *)request originRequestParams:(NSDictionary *)originRequestParams actualRequestParams:(NSDictionary *)actualRequestParams {
    [mutableString appendFormat:@"\n\nHTTP URL:\n\t%@", request.URL];
    [mutableString appendFormat:@"\n\nHTTP Header:\n%@", request.allHTTPHeaderFields ? request.allHTTPHeaderFields : @"\t\t\t\t\tN/A"];
    [mutableString appendFormat:@"\n\nHTTP Origin Params:\n\t%@", originRequestParams];
    [mutableString appendFormat:@"\n\nHTTP Actual Params:\n\t%@", actualRequestParams];
    [mutableString appendFormat:@"\n\nHTTP Body:\n\t%@", [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]];

    NSMutableString *headerString = [[NSMutableString alloc] init];
    [request.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        NSString *header = [NSString stringWithFormat:@" -H \"%@: %@\"", key, obj];
        [headerString appendString:header];
    }];

    [mutableString appendString:@"\n\nCURL:\n\t curl"];
    [mutableString appendFormat:@" -X %@", request.HTTPMethod];
    
    if (headerString.length > 0) {
        [mutableString appendString:headerString];
    }
    if (request.HTTPBody.length > 0) {
        [mutableString appendFormat:@" -d '%@'", [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]];
    }
    [mutableString appendFormat:@" %@", request.URL];
}

@end
